using System;
using System.Data;
using System.IO;
using istIUniDB;
using System.Collections;
using System.Data.OleDb;

namespace Shraga.BLL
{
	/// <summary>
	/// Represents an attachmment (may contain one or many files).
	/// </summary>
	public class Attachment
	{
		#region Data Members
		
		protected ArrayList	m_arrAttachData = null;

		#endregion

		#region Access Methods

		/// <summary>
		/// Access method, get only.
		/// </summary>
		public int NoteId
		{
			get { return((AttachmentData)Attachments[0]).NoteId; }
		}
		/// <summary>
		/// Access method, get only.
		/// </summary>
		public int AttachmentId
		{
			get { return ((AttachmentData)Attachments[0]).AttachmentId; }
		}

		/// <summary>
		/// Access method, get only.
		/// </summary>
		public bool IsFax
		{
			get { return ((AttachmentData)Attachments[0]).IsFax; }
		
		}

		/// <summary>
		/// Access method, get only.
		/// </summary>
		public String Comments
		{
			get { return ((AttachmentData)Attachments[0]).Comments; }
		}
		/// <summary>
		/// Access method, get only.
		/// </summary>		
		public String FileName
		{
			get { return ((AttachmentData)Attachments[0]).FileName; }
		}

		public ArrayList Attachments
		{
			get { return m_arrAttachData; }
		}
		
		#endregion

		#region Constructors

		public Attachment (DataView RawView)
		{
			LoadDataView(RawView);
		}

		/// <summary>
		/// Creates a new attachment (the global data would be put in the first sequance).
		/// </summary>
		/// <param name="ReplyID"></param>
		/// <param name="FileName"></param>
		/// <param name="Comments"></param>
		/// <param name="IsFax"></param>
		/// <param name="Data"></param>
		public Attachment(int NoteId, String FileName, String Comments, bool IsFax, byte[] Data)
		{
			IUniDB udb = UniDBSingleton.Instance().UniDB;									
		
			// Creates the parameters for the insert 
			clsParameter[] arrParams = new clsParameter[5];
			arrParams[0] = new clsParameter(NoteId, OracleTypes.Int32);
			arrParams[1] = new clsParameter(FileName, OracleTypes.Varchar2);
			arrParams[2] = new clsParameter(false, Data, Data.Length, OracleTypes.Blob);
			arrParams[3] = new clsParameter(Comments, OracleTypes.Varchar2);
			arrParams[4] = new clsParameter(Convert.ToInt32(IsFax), OracleTypes.Int32);

			DataTable dtAttachment = udb.RunStoredFunctionReturnDataTable("shraga_attachment.Create_Attachment", ref arrParams);			

			LoadDataView(dtAttachment.DefaultView);
		}				

	
		#endregion

		#region AttachmentData Methods

		/// <summary>
		/// Adds a new attachment data in the sequance.
		/// </summary>
		/// <param name="FileName"></param>
		/// <param name="Comments"></param>
		/// <param name="Data"></param>
		public int AddAttachmentData(byte[] Data, String Comments)
		{
			if (m_arrAttachData == null) m_arrAttachData = new ArrayList();
			return (m_arrAttachData.Add(new AttachmentData(AttachmentId,Data, Comments)));			
		}

		private void LoadDataView(DataView RawView)
		{
			m_arrAttachData = new ArrayList();

			foreach (DataRowView currRow in RawView)
			{
				AttachmentData attch = new AttachmentData(currRow.Row);
				m_arrAttachData.Add(attch);
			}
		}

		#endregion		
	}
	
	/// <summary>
	/// Represents an attachment data of reply attachment.
	/// </summary>
	public class AttachmentData : DBObject
	{
		#region Data Members
		
		protected byte[] m_Data = new byte[0];
		
		#endregion

		#region Access Methods

		/// <summary>
		/// Access methods, get only.
		/// </summary>
		public int NoteId
		{
			get { return base.GetIntData("NOTE_ID"); }
		}
		/// <summary>
		/// Access methods, get only.
		/// </summary>
		public int AttachmentId
		{
			get { return base.PrimaryKey; }
		}

		/// <summary>
		/// Access methods, get only.
		/// </summary>		
		public int SequanceNum
		{
			get { return base.GetIntData("SEQUANCE_NUM"); } 
		}

		/// <summary>
		/// Access methods, get only.
		/// </summary>
		public String FileName
		{
			get { return base.GetStringData("FILE_NAME"); } 
		}

		/// <summary>
		/// Access methods, get only.
		/// </summary>
		public String Comments
		{
			get { return base.GetStringData("COMMENTS"); }
		}

		/// <summary>
		/// Access methods, get only.
		/// </summary>
		public bool IsFax
		{
			get { return base.GetBoolData("IS_FAX"); }
		}

		/// <summary>
		/// Access methods, get only.
		/// </summary>
		/// <remarks>The DataStream is loaded only on first call (on the fly).</remarks>
		public byte[] Data
		{
			get 
			{ 
				// Checks if needs to load the data (the data is read on the fly and not on creation)
				if (m_Data.Length == 0)
				{
					#region Loads the Data
					
					IUniDB udb = UniDBSingleton.Instance().UniDB;						

					// Creates the parameters for the insert 
					clsParameter[] arrParams = new clsParameter[2];
					arrParams[0] = new clsParameter(AttachmentId, OracleTypes.Int32);
					arrParams[1] = new clsParameter(SequanceNum, OracleTypes.Int32);

					// Gets the attachment blob					
					DataTable dtBlob = udb.RunStoredFunctionReturnDataTable("Shraga_Attachment.Get_Attachment_Data", ref arrParams);
					
					m_Data = (byte[])dtBlob.Rows[0]["DATA"];
														
					#endregion				
				}

				return m_Data; 
			}
		}
		
		#endregion

		#region Constructors

		/// <summary>
		/// Constructs the attachment data from raw data.
		/// </summary>
		/// <param name="NoteID"></param>
		/// <param name="ReplyID"></param>
		/// <param name="AttachmentID"></param>
		/// <param name="SequanceNum"></param>
		/// <param name="FileName"></param>
		/// <param name="Comments"></param>
		/// <param name="IsFax"></param>
		public AttachmentData(DataRow RawData)
		:base(RawData)
		{}

		/// <summary>
		/// Create constructor, creates a new attachment data in the attachment sequance.
		/// </summary>
		/// <remarks>The attachment id is always given. 
		///	The responsibility of knowing what is should be is in a higher level.</remarks>
		/// <param name="ReplyID"></param>
		/// <param name="AttachmentID"></param>
		/// <param name="FileName"></param>
		/// <param name="Comments"></param>
		/// <param name="Data"></param>
		public AttachmentData(int AttachmentId, byte[] Data, String Comments)
		{
			IUniDB udb = UniDBSingleton.Instance().UniDB;									

	
			// Adds the parameters for the insert 
			clsParameter[] arrParams = new clsParameter[3];
			arrParams[0] = new clsParameter(AttachmentId, OracleTypes.Int32);
			arrParams[1] = new clsParameter(false, Data, Data.Length, OracleTypes.Blob);
			arrParams[2] = new clsParameter(Comments, OracleTypes.Varchar2);			

			// Inserts the new attachment data
			DataTable dtAttachment = udb.RunStoredFunctionReturnDataTable("Shraga_Attachment.Add_Attachment_Data", ref arrParams);

			base.LoadDataRow(dtAttachment.Rows[0]);
		}

		#endregion
	}
}
